package org.adorsys.aderp.aderplogin.client;

import org.adorsys.aderp.aderplogin.domain.AderpClientDetail;
import org.adorsys.aderp.aderplogin.service.AderpClientDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;

public class ClientDetailServiceClient implements ClientDetailsService {

	@Autowired
	private AderpClientDetailService aderpClientDetailService;
	
	@Override
	public ClientDetails loadClientByClientId(String clientId)
			throws OAuth2Exception {
		if (StringUtils.isBlank(clientId)) {
			throw new InvalidClientException("Client with id not found");
		}

		AderpClientDetail client = aderpClientDetailService
				.findByClientId(clientId);
		if (client == null)
			throw new InvalidClientException("Client with id not found");

		return ClientDetailsHelper.createClientDetails(client);
	}

}
